#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)

ECHO_INFO()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[42m${OUTPUT_FLAG}\033[0m $@"
        else
            echo -ne "${OUTPUT_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[42m${OUTPUT_FLAG}\033[0m $@"
        else
            echo -e "${OUTPUT_FLAG} $@"
        fi
    fi
}

ECHO_QUESTION()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[45m${QUESTION_FLAG}\033[0m $@"
        else
            echo -ne "${QUESTION_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[45m${QUESTION_FLAG}\033[0m $@"
        else
            echo -e "\n${QUESTION_FLAG} $@"
        fi
    fi
}

backup_file()
{
    for conf_file in $@; do
        if [ -f ${conf_file} ]; then
            if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
                echo -e "\033[43m${BACKUP_FLAG}\033[0m Backup file: ${conf_file} -> ${conf_file}.${DATE}."
            else
                echo -e "${BACKUP_FLAG} Backup file: ${conf_file} -> ${conf_file}.${DATE}."
            fi

            cp -f ${conf_file} ${conf_file}.${DATE}
        else
            :
        fi
    done
}

install_pkg()
{
    ECHO_INFO "Installing package(s): $@"
    ${YUM} -y install $@
    if [ X"$?" != X"0" ]; then
        echo -e "Installation failed, please check the terminal output."
        exit 255
    else
        :
    fi
}

remove_pkg()
{
    ECHO_INFO "Removing package(s): $@"
    ${YUM} -y remove $@
    [ X"$?" != X"0" ] && ECHO_INFO "Package removed failed, please check the terminal output."
}

extract_pkg()
{
    if [ X"$2" = X"" ]; then
        DST='.'
    else
        DST="$2"
    fi

    if echo $1 | grep '.tar.gz$' >/dev/null 2>&1 ; then
        ECHO_INFO "Extract package $1 to ${DST}"
        tar zxf $1 -C $DST
    elif echo $1 | grep '.tar.bz2$' >/dev/null 2>&1 ; then
        # Install bzip2 first.
        which bzip2 >/dev/null 2>&1

        if [ X"$?" != X"0" ]; then
            install_pkg bzip2
        else
            :
        fi

        ECHO_INFO "Extract package $1 to ${DST}"
        tar xjf $1 -C $DST
    else
        ECHO_INFO "Unknown archive format."
    fi
}

# Check hardware architecture.
check_arch()
{
    ARCH="$(uname -i)"
    if [ X"${ARCH}" == X"i386" -o X"${ARCH}" == X"x86_64" ]; then
        :
    else
        ECHO_INFO "Your architecture is not supported yet: ${ARCH}."
        ECHO_INFO "${PROG_NAME} support i386 only up to now."
    fi
}

check_user()
{
    # Check special user privilege to execute this script.
    if [ X"$(id -u)" != X"$(id -u ${1})" ]; then
        ECHO_INFO "Please run this script as user: ${1}."
        exit 255
    else
        if [ X"$(id -u)" == X"0" ]; then
            export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
        else
            :
        fi
    fi
}

check_os_version()
{
    :
}

check_dialog()
{
    # -------------------------------------------------------------
    # Use 'dialog' for configuration.
    # -------------------------------------------------------------
    # Install 'dialog' first.
    which dialog >/dev/null 2>&1

    if [ X"$?" != X"0" ]; then
        install_pkg dialog.${ARCH}

        #[ X"$?" != X"0" ] && \
        #    echo "${OUTPUT_FLAG} Install 'dialog' failed. Please install it first and make" && \
        #    echo "${OUTPUT_FLAG} sure your yum client works well." && \
        #    exit 255
    else
        :
    fi
}

# Check necessery privileges/files/dirs.
check_env()
{
    # Check user privilege.
    check_user root

    # Check config tool: dialog.
    check_dialog

    ECHO_INFO -n "Check configuration file: ${CONFIG_FILE}..."
    if [ -f ${CONFIG_FILE} ]; then
        echo -e "\tFound."
        ECHO_QUESTION -n "Use it for mail server setting? [y|N]"
        read ANSWER

        case  $ANSWER in
            Y|y )
                ECHO_INFO "Use configuration file: ${CONFIG_FILE} for mail server setting."
                . ${CONFIG_FILE}

                # Check installation status.
                # After each component installation was completed, there
                # should be a variable in ${STATUS_FILE}, e.g.
                #
                #   export STATUS_PHP_INSTALLATION='DONE'
                #   export STATUS_PHP_CONFIGURATION='DONE'
                #
                if [ -e ${STATUS_FILE} ]; then
                    . ${STATUS_FILE}
                else
                    echo '' > ${STATUS_FILE}
                fi

                if [ ! -e ${TIP_FILE} ]; then
                    cat > ${TIP_FILE} <<EOF
${CONF_MSG}
If you consider here should be put some more infomation, please contact us:
    - http://${PROG_NAME}.googlecode.com/

EOF
                else
                    :
                fi
                ;;
            N|n|* )
                echo "Skip configuration file: ${CONFIG_FILE}."
                . ${CONFIG_VIA_DIALOG}
                ;;
        esac
    else
        echo -e "\tNone."
        . ${CONFIG_VIA_DIALOG}
    fi
}

check_status_before_run()
{
    #
    # Every function will append status info to ${STATUS_FILE}.
    # Format is:
    #   function_name() {:}
    # Status info:
    #   export status_function_name='DONE'
    #
    _status="status_$1"
    if [ X"$(eval echo \$${_status})" != X"DONE" ]; then
        $1
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[46m<<< SKIP >>>\033[0m Skip function: $1."
        else
            echo -e "<<< SKIP >>> Skip function: $1."
        fi
    fi
}

gen_ldap_passwd()
{
    slappasswd -h {MD5} -s "${1}"
}
